<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Feedback;
use Illuminate\Http\Request;
class FeedbackController extends Controller
{
    public function index(){
        return view('admin.feedback.index');
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $model = Feedback::query();
        $user_phone=$request->get('user_phone');
        $content=$request->get('content');
        $status=$request->get('status');
        $timestp1=$request->get('timestp1');
        $timestp2=$request->get('timestp2');
        if(!empty($user_phone)){
            $model = $model->where('user_phone','like','%'.$user_phone.'%');
        }
        if(!empty($content)){
            $model = $model->where('content','like','%'.$content.'%');
        }
        if(!empty($status)){
            $model = $model->where('status','=',$status);
        }
        if(!empty($timestp1)){
            $model = $model->where('timestp','>=',$timestp1);
        }
        if(!empty($timestp2)){
            $model = $model->where('timestp','<=',$timestp2);
        }
        $res = $model->orderBy('id','desc')->paginate($request->get('limit',10))->toArray();

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' =>$res['total'] ,
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function read(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择已读项']);
        }
        $feedback = Feedback::query()->whereIn('id',$ids);
        if ($feedback->update(['status'=>2])){
            return response()->json(['code'=>0,'msg'=>'操作成功']);
        }
        return response()->json(['code'=>1,'msg'=>'操作失败']);
    }
}